﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VSA.Applications.VSA.WebService;

namespace VSA.Applications
{
    public partial class SearchGuideForm : Form
    {
        private EntranceForm mainForm;
        MainService _CompanyService = WebServiceFactory.GetService();
        string[] QueryFields = { "CompanyName", "Telephone1", "Telephone2", "Country", "City", "PostCode", "Continent", "Street", "Activity", "Email", "Website", "Historic", "Comments" };
        string _Sql;
        public SearchGuideForm(EntranceForm mainForm)
        {
            InitializeComponent();
            this.mainForm = mainForm;
            _CompanyService.GetCompanyListCompleted += new GetCompanyListCompletedEventHandler(GetCompanyListCompleted);
        }

        void GetCompanyListCompleted(object sender, GetCompanyListCompletedEventArgs e)
        {
            if (e.Result.Tables[0].Rows.Count > 1 || e.Result.Tables[0].Rows.Count == 0)
            {
                CompanyListForm companyListForm = new CompanyListForm(this, e.Result, _Sql);
                companyListForm.Show();
                this.Hide();
            }
            else if (e.Result.Tables[0].Rows.Count == 1)
            {
                CompanyInfoForm companyInfoForm = new CompanyInfoForm(this, int.Parse(e.Result.Tables[0].Rows[0]["CompanyId"].ToString()));
                companyInfoForm.Show();
                this.Hide();
            }
        }

        private void SearchGuideForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            mainForm.Show();
            this.Dispose();
        }

        private void btnSearchCompany_Click(object sender, EventArgs e)
        {
            List<string> conditions = new List<string>();
            conditions.Add(this.txtCondition1.Text.Trim());
            conditions.Add(this.txtCondition2.Text.Trim());
            conditions.Add(this.txtCondition3.Text.Trim());
            conditions.Add(this.txtCondition4.Text.Trim());

            string sql = "1=1";
            foreach (string field in QueryFields)
            {
                foreach (string condtion in conditions)
                {
                    if (!string.IsNullOrEmpty(condtion))
                    {
                        sql = appendSql(sql, " " + field + " like '%%" + condtion + "%%'");
                    }
                }
            }
            _Sql = sql;
            _CompanyService.GetCompanyListAsync(sql);
        }


        string appendSql(string beforeSql, string endSql)
        {
            if (beforeSql.Equals("1=1"))
                beforeSql += " and";
            else
                beforeSql += " or";
            endSql = beforeSql + endSql;
            return endSql;
        }

        private void btnSearchPerson_Click(object sender, EventArgs e)
        {
            this.Hide();

            StringBuilder sbSqlCompany = new StringBuilder();
            if (!String.IsNullOrEmpty(this.txtCondition1.Text.Trim()))
            {
                sbSqlCompany.Append(" Country LIKE '%" + this.txtCondition1.Text.Trim() + "%'");
                sbSqlCompany.Append(" OR City LIKE '%" + this.txtCondition1.Text.Trim() + "%'");
                sbSqlCompany.Append(" OR Street LIKE '%" + this.txtCondition1.Text.Trim() + "%'");
                sbSqlCompany.Append(" OR Activity LIKE '%" + this.txtCondition1.Text.Trim() + "%'");
                sbSqlCompany.Append(" OR Continent LIKE '%" + this.txtCondition1.Text.Trim() + "%'");
            }
            if (!String.IsNullOrEmpty(this.txtCondition2.Text.Trim()))
            {
                if (String.IsNullOrEmpty(sbSqlCompany.ToString()))
                {
                    sbSqlCompany.Append(" Country LIKE '%" + this.txtCondition2.Text.Trim() + "%'");
                }
                else
                {
                    sbSqlCompany.Append(" OR Country LIKE '%" + this.txtCondition2.Text.Trim() + "%'");
                }
                sbSqlCompany.Append(" OR City LIKE '%" + this.txtCondition2.Text.Trim() + "%'");
                sbSqlCompany.Append(" OR Street LIKE '%" + this.txtCondition2.Text.Trim() + "%'");
                sbSqlCompany.Append(" OR Activity LIKE '%" + this.txtCondition2.Text.Trim() + "%'");
                sbSqlCompany.Append(" OR Continent LIKE '%" + this.txtCondition2.Text.Trim() + "%'");
            }
            if (!String.IsNullOrEmpty(this.txtCondition3.Text.Trim()))
            {
                if (String.IsNullOrEmpty(sbSqlCompany.ToString()))
                {
                    sbSqlCompany.Append(" Country LIKE '%" + this.txtCondition3.Text.Trim() + "%'");
                }
                else
                {
                    sbSqlCompany.Append(" OR Country LIKE '%" + this.txtCondition3.Text.Trim() + "%'");
                }
                sbSqlCompany.Append(" OR City LIKE '%" + this.txtCondition3.Text.Trim() + "%'");
                sbSqlCompany.Append(" OR Street LIKE '%" + this.txtCondition3.Text.Trim() + "%'");
                sbSqlCompany.Append(" OR Activity LIKE '%" + this.txtCondition3.Text.Trim() + "%'");
                sbSqlCompany.Append(" OR Continent LIKE '%" + this.txtCondition3.Text.Trim() + "%'");
            }
            if (!String.IsNullOrEmpty(this.txtCondition4.Text.Trim()))
            {
                if (String.IsNullOrEmpty(sbSqlCompany.ToString()))
                {
                    sbSqlCompany.Append(" Country LIKE '%" + this.txtCondition4.Text.Trim() + "%'");
                }
                else
                {
                    sbSqlCompany.Append(" OR Country LIKE '%" + this.txtCondition4.Text.Trim() + "%'");
                }
                sbSqlCompany.Append(" OR City LIKE '%" + this.txtCondition4.Text.Trim() + "%'");
                sbSqlCompany.Append(" OR Street LIKE '%" + this.txtCondition4.Text.Trim() + "%'");
                sbSqlCompany.Append(" OR Activity LIKE '%" + this.txtCondition4.Text.Trim() + "%'");
                sbSqlCompany.Append(" OR Continent LIKE '%" + this.txtCondition4.Text.Trim() + "%'");
            }

            StringBuilder sbSql = new StringBuilder();
            if (!String.IsNullOrEmpty(this.txtCondition1.Text.Trim()))
            {
                sbSql.Append(" FamilyName LIKE '%" + this.txtCondition1.Text.Trim() + "%'");
                sbSql.Append(" OR GivenName LIKE '%" + this.txtCondition1.Text.Trim() + "%'");
                sbSql.Append(" OR Functions LIKE '%" + this.txtCondition1.Text.Trim() + "%'");
                sbSql.Append(" OR DirectTelephone LIKE '%" + this.txtCondition1.Text.Trim() + "%'");
                sbSql.Append(" OR DirectEmail LIKE '%" + this.txtCondition1.Text.Trim() + "%'");
                sbSql.Append(" OR MobileTelephone LIKE '%" + this.txtCondition1.Text.Trim() + "%'");
                sbSql.Append(" OR Comments LIKE '%" + this.txtCondition1.Text.Trim() + "%'");
                sbSql.Append(" OR UpdateBy LIKE '%" + this.txtCondition1.Text.Trim() + "%'");
                sbSql.Append(" OR CreateBy LIKE '%" + this.txtCondition1.Text.Trim() + "%'");
            }

            if (!String.IsNullOrEmpty(this.txtCondition2.Text.Trim()))
            {
                if (String.IsNullOrEmpty(sbSql.ToString()))
                {
                    sbSql.Append(" FamilyName LIKE '%" + this.txtCondition2.Text.Trim() + "%'");
                }
                else
                {
                    sbSql.Append(" OR FamilyName LIKE '%" + this.txtCondition2.Text.Trim() + "%'");
                }
                sbSql.Append(" OR GivenName LIKE '%" + this.txtCondition2.Text.Trim() + "%'");
                sbSql.Append(" OR Functions LIKE '%" + this.txtCondition2.Text.Trim() + "%'");
                sbSql.Append(" OR DirectTelephone LIKE '%" + this.txtCondition2.Text.Trim() + "%'");
                sbSql.Append(" OR DirectEmail LIKE '%" + this.txtCondition2.Text.Trim() + "%'");
                sbSql.Append(" OR MobileTelephone LIKE '%" + this.txtCondition2.Text.Trim() + "%'");
                sbSql.Append(" OR Comments LIKE '%" + this.txtCondition2.Text.Trim() + "%'");
                sbSql.Append(" OR UpdateBy LIKE '%" + this.txtCondition2.Text.Trim() + "%'");
                sbSql.Append(" OR CreateBy LIKE '%" + this.txtCondition2.Text.Trim() + "%'");
            }

            if (!String.IsNullOrEmpty(this.txtCondition3.Text.Trim()))
            {
                if (String.IsNullOrEmpty(sbSql.ToString()))
                {
                    sbSql.Append(" FamilyName LIKE '%" + this.txtCondition3.Text.Trim() + "%'");
                }
                else
                {
                    sbSql.Append(" OR FamilyName LIKE '%" + this.txtCondition3.Text.Trim() + "%'");
                }
                sbSql.Append(" OR GivenName LIKE '%" + this.txtCondition3.Text.Trim() + "%'");
                sbSql.Append(" OR Functions LIKE '%" + this.txtCondition3.Text.Trim() + "%'");
                sbSql.Append(" OR DirectTelephone LIKE '%" + this.txtCondition3.Text.Trim() + "%'");
                sbSql.Append(" OR DirectEmail LIKE '%" + this.txtCondition3.Text.Trim() + "%'");
                sbSql.Append(" OR MobileTelephone LIKE '%" + this.txtCondition3.Text.Trim() + "%'");
                sbSql.Append(" OR Comments LIKE '%" + this.txtCondition3.Text.Trim() + "%'");
                sbSql.Append(" OR UpdateBy LIKE '%" + this.txtCondition3.Text.Trim() + "%'");
                sbSql.Append(" OR CreateBy LIKE '%" + this.txtCondition3.Text.Trim() + "%'");
            }

            if (!String.IsNullOrEmpty(this.txtCondition4.Text.Trim()))
            {
                if (String.IsNullOrEmpty(sbSql.ToString()))
                {
                    sbSql.Append(" FamilyName LIKE '%" + this.txtCondition4.Text.Trim() + "%'");
                }
                else
                {
                    sbSql.Append(" OR FamilyName LIKE '%" + this.txtCondition4.Text.Trim() + "%'");
                }
                sbSql.Append(" OR GivenName LIKE '%" + this.txtCondition4.Text.Trim() + "%'");
                sbSql.Append(" OR Functions LIKE '%" + this.txtCondition4.Text.Trim() + "%'");
                sbSql.Append(" OR DirectTelephone LIKE '%" + this.txtCondition4.Text.Trim() + "%'");
                sbSql.Append(" OR DirectEmail LIKE '%" + this.txtCondition4.Text.Trim() + "%'");
                sbSql.Append(" OR MobileTelephone LIKE '%" + this.txtCondition4.Text.Trim() + "%'");
                sbSql.Append(" OR Comments LIKE '%" + this.txtCondition4.Text.Trim() + "%'");
                sbSql.Append(" OR UpdateBy LIKE '%" + this.txtCondition4.Text.Trim() + "%'");
                sbSql.Append(" OR CreateBy LIKE '%" + this.txtCondition4.Text.Trim() + "%'");
            }

            String companyIds = String.Empty;
            String personIds = String.Empty;
            DataSet dsCompany = WebServiceFactory.GetService().GetCompanyList(sbSqlCompany.ToString());

            if (dsCompany.Tables != null)
            {
                foreach (DataRow dr in dsCompany.Tables[0].Rows)
                {
                    if (String.IsNullOrEmpty(companyIds))
                    {
                        companyIds = dr["CompanyId"].ToString();
                    }
                    else
                    {
                        companyIds += "," + dr["CompanyId"].ToString();
                    }
                }

                DataSet dsCP = new DataSet();
                if (!String.IsNullOrEmpty(companyIds))
                {
                    dsCP = WebServiceFactory.GetService().GetCompanyPersonMappingList(" CompanyId IN (" + companyIds + ")");
                }

                if (dsCP.Tables != null)
                {
                    foreach (DataRow dr in dsCP.Tables[0].Rows)
                    {
                        if (String.IsNullOrEmpty(personIds))
                        {
                            personIds = dr["PersonId"].ToString();
                        }
                        else
                        {
                            personIds += "," + dr["PersonId"].ToString();
                        }
                    }
                }
            }

            if(!String.IsNullOrEmpty(personIds))
            {
                if (String.IsNullOrEmpty(sbSql.ToString()))
                {
                    sbSql.Append(" PersonId IN (" + personIds + ")");
                }
                else
                {
                    sbSql.Append(" OR PersonId IN (" + personIds + ")");
                }
            }

            DataSet resultSet = WebServiceFactory.GetService().GetContactPersonList(sbSql.ToString());

            if (resultSet.Tables[0].Rows.Count == 1)
            {
                PersonInfoForm personInfoForm = new PersonInfoForm(Convert.ToInt32(resultSet.Tables[0].Rows[0]["PersonId"]));
                personInfoForm.CallType = 2;
                personInfoForm.Entrance = this.mainForm;
                personInfoForm.Show();
                this.Dispose();
            }
            else
            {
                PersonListForm personListForm = new PersonListForm(this, resultSet);
                personListForm.Show();
            }
        }
    }
}
